---
title: "Multi-Level Conceptual Replication of Fulmer et al. (2010): Models"
author: "Matthew Samson"
date: "19 January 2015"
output: html_document
---

Require packages.

```{r}

install.packages(c("psych","nlme", "reshape", "ICC", "plyr", "data.table"))

require("psych"); require("nlme"); require("reshape"); require("ICC"); require("plyr"); require("data.table")

```

Set Working Directory and load data.

```{r, echo=TRUE}

setwd("~/mjs268/2_Direct Replication of Fulmer et al. (2010)/4_Data for Analysis")
bigfive <- read.csv("1_Big Five By Country.csv")
collectivism <- read.csv("2_Invdividualism and Collectivism Scores by Country.csv")
ics2001 <- read.csv("3_International College Survey 2001 Final.csv")

```

Adjust for reverse coding and aggregate relevant variables in ics2001.

```{r}

#Reverse coding
ics2001$ext2 <- 6 - ics2001$ext2
ics2001$ext3 <- 6 - ics2001$ext3
ics2001$ext6 <- 6 - ics2001$ext6

#Average Extroversion
ics2001$ext_tot <- (ics2001$ext1 + ics2001$ext2 + ics2001$ext3 + ics2001$ext4 + ics2001$ext5 + ics2001$ext6)/6

#Average Subjective Well-Being
ics2001$swl_tot <- (ics2001$swl1 + ics2001$swl2 + ics2001$swl3 + ics2001$swl4 + ics2001$swl5)/5

#Average Positive Emotions in Past Week
ics2001$pos_tot <- (ics2001$pos1 + ics2001$pos2 + ics2001$pos3 + ics2001$pos4 + ics2001$pos5)/5

#Average Collectivism (for Model 2)
ics2001$col_tot <- (ics2001$col1 + ics2001$col2 + ics2001$col3)/3

```

Correlation between subjective Well-Being; and, Positive Emotions and Life Happiness.

```{r}

TEMP1 <- na.omit(ics2001[,c("country", "ext_tot", "swl_tot", "pos_tot", "hap_life")])

cor.test(TEMP1$swl_tot, TEMP1$pos_tot)
cor.test(TEMP1$swl_tot, TEMP1$hap_life)

```

Sex and age of full sample.

```{r}

#Percentage of women in full sample. 
table(ics2001$sex)
6019/(6019+3978)

#Age of participants
#Recode age into numeric variable
ics2001$age_num[ics2001$age=="under 18" & !is.na(ics2001$age)]  <- 0
ics2001$age_num[ics2001$age=="18-19" & !is.na(ics2001$age)]  <- 1
ics2001$age_num[ics2001$age=="20-21" & !is.na(ics2001$age)]  <- 2
ics2001$age_num[ics2001$age=="22-23" & !is.na(ics2001$age)]  <- 3
ics2001$age_num[ics2001$age=="24-25" & !is.na(ics2001$age)]  <- 4
ics2001$age_num[ics2001$age=="26-27" & !is.na(ics2001$age)]  <- 5
ics2001$age_num[ics2001$age=="28 and over" & !is.na(ics2001$age)]  <- 6
#Find median age
summary(ics2001$age_num)

```

Internal consistency and ICC for Extroversion; and, Internal Consistency for Collectivism and Subjective Well-Being.

```{r}

#Extroversion Alpha
TEMP2 <- na.omit(ics2001[,c("country", "ext1", "ext2", "ext3", "ext4", "ext5", "ext6")])
alpha(TEMP2)

#Extroversion ICC
TEMP3 <- na.omit(ics2001[,c("country", "ext_tot")])
ICCbareF(country, ext_tot, data=TEMP3)

#Satisfaction with Life Alpha
TEMP4 <- na.omit(ics2001[,c("col1", "col2", "col3")])
alpha(TEMP4)

#Satisfaction with Life Alpha
TEMP5 <- na.omit(ics2001[,c("swl1", "swl2", "swl3", "swl4", "swl5")])
alpha(TEMP5)

```

Descriptives for Individaul Extroversion and Subjective Well-Being.

```{r}

hist(ics2001$ext_tot)
describe(ics2001$ext_tot)

hist(ics2001$swl_tot)
describe(ics2001$swl_tot)

```

Descriptives for culture-level extroversion. 

```{r}

#ICS2001 Extroversion
country.means <- data.table(TEMP3)
country.means <- data.frame(country.means[,list(country.ext_tot=mean(ext_tot,na.rm=T)),by=country])
hist(country.means$country.ext_tot)
describe(country.means$country.ext_tot)

#McCrae et al. (2005) Extroversion
hist(bigfive$extrov)
describe(bigfive$extrov)

write.csv(country.means, "country.means.csv")

```

Descriptives for culture-level collectivism. 

```{r}

#ICS2001 Collectivism 
TEMP6 <- na.omit(ics2001[,c("country", "col_tot")])
collectivism.means <- data.table(TEMP6)
collectivism.means <- data.frame(collectivism.means[,list(collectivism.col_tot=mean(col_tot,na.rm=T)),by=country])
hist(collectivism.means$collectivism.col_tot)
describe(collectivism.means$collectivism.col_tot)

#House et al. (2004) Collectivism
TEMP6 <- merge(collectivism.means, collectivism, by="country")
hist(TEMP6$SOC.PRAC.Collectivism..GLOBE.)
describe(TEMP6$SOC.PRAC.Collectivism..GLOBE.)

```
